import axios from 'axios'
import store from '@/store'
import JSONBig from 'json-bigint'
// const jsonStr = '{"art_id":124578978458578854}'
// console.log(JSON.parse(jsonStr))
// console.log(JSONBig.parse(jsonStr))
// console.log(JSONBig.parse(jsonStr).art_id.toString())

const request = axios.create({
  baseURL: 'http://toutiao.itheima.net',
  // baseURL: 'http://toutiao-app.itheima.net',
  // baseURL: 'http://192.168.17.27:8000',
  transformResponse: [
    function(data) {
      try {
        return JSONBig.parse(data)
      } catch (err) {
        return data
      }
    },
  ],
})
// Add a request interceptor
request.interceptors.request.use(
  config => {
    // Do something before request is sent
    const { user } = store.state
    if (user && user.token) {
      config.headers.Authorization = `Bearer ${user.token}`
    }
    return config
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error)
  }
)
export default request
